package controllers;

import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import views.html.pageErreurConnexion;
import views.html.ConsoleAdmin.*;

import models.*;

/**
 * Manage a database of niveauQuestions
 */
public abstract class AdminNiveauQuestion extends Controller {

	/**
	 * This result directly redirect to application home.
	 */
	public final static Result GO_HOME = redirect(routes.AdminNiveauQuestion
			.listNiveauQuestion(0, "id_niveau_q", "asc", ""));

	/**
	 * Handle default path requests, redirect to niveauQuestions list
	 */
	public static Result indexNiveauQuestion() {
		if (Application.estAdmin()) {
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the paginated list of niveauQuestions.
	 * 
	 * @param page
	 *            Current page number (starts from 0)
	 * @param sortBy
	 *            Column to be sorted
	 * @param order
	 *            Sort order (either asc or desc)
	 * @param filter
	 *            Filter applied on niveauQuestion names
	 */
	public static Result listNiveauQuestion(final int page,
			final String sortBy, final String order, final String filter) {
		if (Application.estAdmin()) {
			return ok(listNiveauQuestion.render(
					NiveauQuestion.page(page, 10, sortBy, order, filter),
					sortBy, order, filter));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'edit form' of a existing NiveauQuestion.
	 * 
	 * @param id
	 *            Id of the niveauQuestion to edit
	 */
	public static Result editNiveauQuestion(final Long id) {
		if (Application.estAdmin()) {
			final Form<NiveauQuestion> niveauQuestionForm = form(
					NiveauQuestion.class).fill(NiveauQuestion.findById(id));
			return ok(editNiveauQuestion.render(id, niveauQuestionForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'edit form' submission
	 * 
	 * @param id
	 *            Id of the niveauQuestion to edit
	 */
	public static Result updateNiveauQuestion(final Long id) {
		if (Application.estAdmin()) {
			final Form<NiveauQuestion> niveauQuestionForm = form(
					NiveauQuestion.class).bindFromRequest();
			if (niveauQuestionForm.hasErrors()) {
				return badRequest(editNiveauQuestion.render(id,
						niveauQuestionForm));
			}

			niveauQuestionForm.get().update(id);
			flash("success", "NiveauQuestion "
					+ niveauQuestionForm.get().getLibelleNiveau()
					+ " has been updated");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'new niveauQuestion form'.
	 */
	public static Result createNiveauQuestion() {
		if (Application.estAdmin()) {
			final Form<NiveauQuestion> niveauQuestionForm = form(NiveauQuestion.class);
			return ok(createNiveauQuestion.render(niveauQuestionForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'new niveauQuestion form' submission
	 */
	public static Result saveNiveauQuestion() {
		if (Application.estAdmin()) {
			final Form<NiveauQuestion> niveauQuestionForm = form(
					NiveauQuestion.class).bindFromRequest();
			if (niveauQuestionForm.hasErrors()) {
				return badRequest(createNiveauQuestion
						.render(niveauQuestionForm));
			}
			niveauQuestionForm.get().save();
			flash("success", "NiveauQuestion "
					+ niveauQuestionForm.get().getLibelleNiveau()
					+ " has been created");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle niveauQuestion deletion
	 */
	public static Result deleteNiveauQuestion(final Long id) {
		if (Application.estAdmin()) {
			NiveauQuestion.delete(id);
			flash("success", "NiveauQuestion has been deleted");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

}
